package org.n.shop.service.impl;

import lombok.AllArgsConstructor;
import org.n.shop.domain.Do.SelectCommodityDetailDo;
import org.n.shop.domain.entity.Commodity;
import org.n.shop.domain.vo.ListVo;
import org.n.shop.mapper.CommodityMapper;
import org.n.shop.result.RestResult;
import org.n.shop.result.RestResultBuilder;
import org.n.shop.service.CommodityService;
import org.n.shop.util.MinioUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class CommodityServiceImpl implements CommodityService {
    private CommodityMapper commodityMapper;
    private MinioUtil minioUtil;

    @Override
    public RestResult all(int page, int pageSize, String key, int orderBy, int type) {
        List<Commodity> commodityList = commodityMapper.all(page * pageSize, pageSize, key, orderBy, type);

        for (Commodity commodity : commodityList) {
            commodity.setImage(minioUtil.getObject(commodity.getImage()));
        }

        return new RestResultBuilder<ListVo>().success(
                ListVo.builder()
                        .list(commodityList)
                        .total(commodityMapper.total(key, type))
                        .build());
    }

    @Override
    public RestResult selectById(int id) {
        SelectCommodityDetailDo selectCommodityDetailDo = commodityMapper.selectById(id);

        selectCommodityDetailDo.setIntroduce(minioUtil.getObject(selectCommodityDetailDo.getIntroduce()));
        selectCommodityDetailDo.setImage(minioUtil.getObject(selectCommodityDetailDo.getImage()));

        return new RestResultBuilder().success(
                selectCommodityDetailDo
        );
    }
}
